<?php $title = '版块'; require './header.php'; ?>

<?php
$category = $hlx->getCategoryInfo($_REQUEST['cat_id']);
$category['status'] or die('版块不存在！');
$topAndNoticePosts = $hlx->getCategoryTopPostList($category['categoryID']);
$posts = $hlx->getCategoryPostList($category['categoryID'],$_REQUEST['tag_id'],$_REQUEST['sort'],50,0);
?>

<div class="container-fluid">
	<div class="media border p-3">
		<img src="<?php echo $category['icon']; ?>" alt="版块图标" class="mr-3 rounded" style="width:60px;">
		<div class="media-body">
			<h4><?php echo $category['title']; ?></h4>
			<div class="float-right">
				<?php if(! $hlx->signinCheck($category['categoryID'])) { ?>
				<button class="btn btn-warning" id="signin">签到</button>
				<?php } else { ?>
				<button class="btn btn-warning" disabled>已签到</button>
				<?php } ?>
			</div>
			<p>
				<b>热度</b> <?php echo $category['viewCount']; ?>&nbsp;&nbsp;
				<b>话题</b> <?php echo $category['postCount']; ?>
				<br>
				<?php echo $category['description'] ?: '这个版块没有描述'; ?>
			</p>
			<hr>
			<div>
				<h5>版主</h5>
				<?php foreach($category['moderator'] as $user) { ?>
				<img src="<?php echo $user['avatar']; ?>" class="mr-3 border rounded-circle" width="50px">
				<?php } ?>
			</div>
			<hr>
			<div>
				<h5>版规</h5>
				<div id="rule" class="collapse"><?php echo nl2br($category['rule']); ?></div>
				<a href="#rule" data-toggle="collapse">展开/收起</a>
			</div>
		</div>
	</div>
	<div class="mt-3">
		<table class="table">
			<?php foreach($topAndNoticePosts as $post) { ?>
			<tr>
				<td>
					<?php if($post['notice']) { ?>
					<span class="badge badge-danger">公告</span>
					<?php } else { ?>
					<span class="badge badge-warning text-light">置顶</span>
					<?php } ?>
					
					<a href="./post.php?post_id=<?php echo $post['postID']; ?>" class="text-dark"><?php echo $post['title']; ?></a>
				</td>
			</tr>
			<?php } ?>
		</table>
	</div>
	<div class="mt-3">
		<div>
			<span>
				排序：
				<a href="<?php echo goParam(['sort' => 0]); ?>" class="<?php echo $_REQUEST['sort'] == 0 ? 'text-muted' : ''; ?>">按回复</a> |
				<a href="<?php echo goParam(['sort' => 1]); ?>" class="<?php echo $_REQUEST['sort'] == 1 ? 'text-muted' : ''; ?>">按发布</a> |
				<a href="<?php echo goParam(['sort' => 2]); ?>" class="<?php echo $_REQUEST['sort'] == 2 ? 'text-muted' : ''; ?>">按精华</a>
			</span>
			<span class="float-right">
				分区：
				<a href="<?php echo goParam(['tag_id' => 0]); ?>" class="<?php echo $_REQUEST['tag_id'] == 0 ? 'text-muted' : ''; ?>">全部</a>
				<?php foreach($category['tags'] as $tag) { ?>
				| <a href="<?php echo goParam(['tag_id' => $tag['tagID']]); ?>" class="<?php echo $_REQUEST['tag_id'] == $tag['tagID'] ? 'text-muted' : ''; ?>"><?php echo $tag['tagName']; ?></a>
				<?php } ?>
			</span>
		</div>
		<table class="table">
			<thead class="thead-light">
				<tr>
					<th width="10%">帖子ID</th>
					<th width="40%">标题</th>
					<th width="15%">发布时间</th>
					<th width="25%">作者</th>
					<th width="10%">评论/浏览</th>
				</tr>
			</thead>
			<tbody id="postList">
				<?php foreach($posts as $post) { ?>
				<tr onclick="javascript:location.href='./post.php?post_id=<?php echo $post['postID']; ?>';">
					<td><span class="text-info"><?php echo $post['postID']; ?></span></td>
					<td>
						<?php if($post['score']) { ?>
						<span class="badge badge-danger"><?php echo ($post['score'] > 0 ? '+' : '') . $post['score']; ?></span>
						<?php } ?>
						<?php if(time() - $post['createTime'] / 1000 <= 12 * 60 * 60) { ?>
						<span class="badge badge-success">新</span>
						<?php } ?>
						<?php if($post['commentCount'] >= 200) { ?>
						<span class="badge badge-danger">热</span>
						<?php } ?>
						<?php if($post['isGood']) { ?>
						<span class="badge badge-warning text-light">精</span>
						<?php } ?>
						<?php if($post['isAuthention']) { ?>
						<span class="badge badge-info">认</span>
						<?php } ?>
						<?php echo $post['title']; ?>
					</td>
					<td><?php echo date('Y-m-d H:i:s',$post['createTime'] / 1000); ?></td>
					<td>
						<?php echo $post['user']['nick']; ?>
						<span class="text-secondary">（<?php echo $post['user']['userID']; ?>）</span>
					</td>
					<td>
						<span class="text-primary"><?php echo $post['commentCount']; ?></span> /
						<?php echo $post['hit']; ?>
					</td>
				</tr>
				<?php } ?>
			</tbody>
		</table>
		<p style="display:none" id="start"><?php echo $post['start']; ?></p>
		<div class="text-center text-muted" id="load">
			继续加载
		</div>
	</div>
</div>
<script>
$('#signin').click(function() {
	$.post('./ajax/signin.php',{cat_id:<?php echo $category['categoryID']; ?>},function(data) {
		alert('签到成功！');
		$('#signin').text('已签到');
		$('#signin').attr({'disabled':'disabled','id':''});
	});
});
$('#load').click(function() {
	$.post('./ajax/categoryPostList.php',{cat_id:<?php echo $category['categoryID']; ?>,tag_id:<?php echo $_REQUEST['tag_id'] ?? 0; ?>,sort:<?php echo $_REQUEST['sort'] ?? 0; ?>,start:$('#start').text()},function(data) {
		data = JSON.parse(data);
		$.each(data,function(key,value) {
			$('#start').text(value.start);
			
			var html = '';
			html += '<tr onclick="javascript:location.href=\'./post.php?post_id='+value.postID+'\';">';
			html += '<td><span class="text-info">'+value.postID+'</span></td>';
			html += '<td>';
			html += new Date() - value.createTime <= 12*60*60*1000 ? '<span class="badge badge-success">新</span>' : '';
			html += value.commentCount >= 200 ? '<span class="badge badge-danger">热</span>' : '';
			html += value.isGood ? '<span class="badge badge-warning text-light">精</span>' : '';
			html += value.isAuthention ? '<span class="badge badge-info">认</span>' : '';
			html += value.title;
			html += '</td>';
			html += '<td>'+timestampToTime(value.createTime)+'</td>';
			html += '<td>';
			html += value.user.nick;
			html += '<span class="text-secondary">（'+value.user.userID+'）</span>';
			html += '</td>';
			html += '<td><span class="text-primary">'+value.commentCount+'</span> / '+value.hit+'</td>';
			html += '</tr>';
			
			$('#postList').append(html);
		});
	});
});

function timestampToTime(timestamp) {
	var date = new Date(timestamp);
	
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
	var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate() + " ";
	var h = " " + (date.getHours() < 10 ? " 0" + date.getHours() : date.getHours()) + ":";
	var m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
	var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
	
	return Y + M + D + h + m + s;
}
</script>
</body>
</html>
